package com.lsk.mobile;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.util.MD5;
import com.lsk.util.Syslog;
import com.lsk.util.UuidUtil;

import java.util.Date;

/**
 * Created by Administrator on 2018/7/18 0018.
 */
public class SaveMemberThread implements Runnable{
    private String orgId;
    private String openId;
    private String unionId;
    private String name;
    private String tel;
    private String parentId;
    private String heardUrl;
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getHeardUrl() {
        return heardUrl;
    }

    public void setHeardUrl(String heardUrl) {
        this.heardUrl = heardUrl;
    }

    public SaveMemberThread() {
    }

    public String getOrgId() {
        return orgId;
    }

    public void setOrgId(String orgId) {
        this.orgId = orgId;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }
    @Override
    public void run() {
        String selSql="SELECT *from yx_member where orgId='"+this.orgId+"' and unionId='"+this.unionId+"'";
        Record record= Db.findFirst(selSql);
        String id=UuidUtil.get32UUID();
        if (record==null){//会员为空
            Record memberRecord=new Record().set("id",id ).set("orgId",this.orgId).set("grade",0)
                    .set("openId",this.openId).set("unionId",this.unionId).set("createTime",new Date())
                    .set("heardUrl",this.heardUrl).set("name",this.name).set("parentId",this.parentId);
            Db.save("yx_member","id",memberRecord);

        }else {
            id=record.getStr("id");
        }
        Record tree=new Record().set("memberId",id);
            /*一级父类*/
        String oneSql="SELECT *from yx_member where id='";
        Record one=Db.findFirstByCache("oneDay", MD5.md5(oneSql+this.parentId+"'"),oneSql+this.parentId+"'");
        if (one!=null){//1级
            String oneId=one.getStr("id");
            String oneParentId=one.getStr("parentId");
            tree.set("oneParentId",oneId);
            Record two=Db.findFirstByCache("oneDay", MD5.md5(oneSql+oneParentId+"'"),oneSql+oneParentId+"'");
            if (two!=null){//2级
                String twoId=two.getStr("id");
                String twoParentId=two.getStr("parentId");
                tree.set("twoParentId",twoId);
                Record three=Db.findFirstByCache("oneDay", MD5.md5(oneSql+twoParentId+"'"),oneSql+twoParentId+"'");
                if (three!=null){//3级
                    String threeId=three.getStr("id");
                    String threeParentId=three.getStr("parentId");
                    tree.set("threeParentId",threeId);
                    Record four=Db.findFirstByCache("oneDay", MD5.md5(oneSql+threeParentId+"'"),oneSql+threeParentId+"'");
                    if (four!=null){//4级
                        String fourId=four.getStr("id");
                        String fourParentId=four.getStr("parentId");
                        tree.set("fourParentId",fourId);
                        Record five=Db.findFirstByCache("oneDay", MD5.md5(oneSql+fourParentId+"'"),oneSql+fourParentId+"'");
                        if (five!=null){//5级
                            String fiveId=five.getStr("id");
                            three.set("fiveParentId",fiveId);
                        }
                    }
                }
            }
        }
        try {
            Db.save("yx_member_tree","memberId",tree);
        }catch (Exception e){
            Syslog.info("执行修改");
            Db.update("yx_member_tree","memberId",tree);
        }
    }
}
